package com.shntec.temp.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.shntec.bp.json.JSONArray;
import com.shntec.bp.json.JSONException;
import com.shntec.bp.json.JSONObject;

import com.shntec.bp.common.ShntecBaseAction;
import com.shntec.bp.common.ShntecErrorCode;
import com.shntec.bp.common.JSONMessageConstant;
import com.shntec.bp.common.RequestMessageParser;
import com.shntec.bp.common.ResponseMessageGenerator;
import com.shntec.bp.util.DatabaseManager;
import com.shntec.bp.util.ShntecLogger;
import com.shntec.temp.util.MySQLConn;

public class GetMeetingAction extends ShntecBaseAction {

	public GetMeetingAction () {
		super.actionCode = 20004;
		super.actionName = "GetMeetingAction";
		super.actionDescription = "获取会议记录信息";
		// No need authentication
		super.needAuthenticationCheck = false;
	}

	@Override
	public ResponseMessageGenerator handleAction(RequestMessageParser parser) {

		ResponseMessageGenerator generator = new ResponseMessageGenerator();

		JSONObject payload = null;
		JSONObject parameter = parser.getParameterJsonObject();
		
		
		if (parameter == null) {

			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER);
		}
		
		int mid = -1;
		
		try {
			mid = parameter.getInt("mid");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"mid\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"mid\"");
		}

		MySQLConn mysql = new MySQLConn();
		Connection conn = null;
		payload = new JSONObject();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = mysql.getConn();
			pstmt = conn.prepareStatement("select * from temp_meeting where mid = ?");
			pstmt.setInt(1, mid);
			
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				
				payload.put("today", rs.getString("todayplans"));
				payload.put("com", rs.getString("completion"));
				payload.put("question", rs.getString("question"));
				
				return generator.toSuccess(parser, payload);
			}else{
				return generator.toError(parser, 20021, "没有找到指定的会议记录");
			}
			
			
		} catch (ClassNotFoundException e) {
			ShntecLogger.logger.error("ClassNotFoundException" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"ClassNotFoundException");
		} catch (SQLException e) {
			ShntecLogger.logger.error("SQLException");
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"SQLException");
		} catch (JSONException e) {
			ShntecLogger.logger.error("JSONException");
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"JSONException");
		}finally{
			if(conn != null){
				
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			if(pstmt != null){
				try {
					pstmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
	}

}
